<script setup lang="ts">
import { onMounted, ref } from 'vue'
import * as echarts from 'echarts'
import 'echarts-liquidfill'

const chartsRef = ref<any>()

const rank = {
  name: ['故宫博物馆', '八达岭长城', '颐和园', '恭王府', '天坛'],
  data: [100, 90, 80, 70, 60],
}

const initCharts = () => {
  const myCharts = echarts.init(chartsRef.value)

  const itemColor1 = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
    {
      offset: 0,
      color: 'rgb(0, 221, 255)',
    },
    {
      offset: 1,
      color: 'rgb(77, 119, 255)',
    },
  ])

  const itemColor2 = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
    {
      offset: 0,
      color: 'rgb(255, 191, 0)',
    },
    {
      offset: 1,
      color: 'rgb(224, 62, 76)',
    },
  ])

  const options = {
    title: {
      show: false,
    },
    textStyle: {
      color: '#fff',
    },
    grid: {
      top: '10%',
      bottom: '3%',
      left: '1%',
      right: '1%',
      containLabel: true,
    },
    tooltip: {
      trigger: 'axis',
    },
    xAxis: {
      type: 'category',
      data: rank.name,
      axisLabel: {
        interval: 0,
      },
    },
    yAxis: {
      type: 'value',
      splitLine: {
        show: false,
      },
    },
    series: [
      {
        name: '热度',
        type: 'bar',
        data: rank.data,
        showBackground: true,
        backgroundStyle: {
          opacity: 0.2,
          color: '#fff',
        },
        itemStyle: {
          color: itemColor1,
          borderRadius: [5, 5, 0, 0],
        },
      },
      {
        name: '热度',
        type: 'line',
        data: rank.data,
        itemStyle: {
          color: itemColor2,
        },
      },
    ],
  }
  myCharts.setOption(options)
}
onMounted(() => {
  initCharts()
})
</script>

<template>
  <div class="data">
    <div class="top">
      <p class="title">热门景区排行</p>
    </div>
    <div class="charts" ref="chartsRef"></div>
  </div>
</template>

<style scoped lang="scss">
$title-height: 32px;

.data {
  flex: 1.2;

  .top {
    margin-bottom: 40px;
    font-size: 20px;
    line-height: $title-height;
    color: #fff;

    .title {
      &::after {
        top: $title-height;
        left: 0;
        display: block;
        width: 68px;
        height: 7px;
        content: '';
        background: url('@/assets/images/data_screen/figure.png') no-repeat;
        background-size: cover;
      }
    }
  }

  .charts {
    align-items: center;
    width: 100%;
    height: 300px;
  }
}
</style>
